package com.paomi.client.base;

import android.content.Context;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.hannesdorfmann.mosby3.mvp.MvpFragment;
import com.hannesdorfmann.mosby3.mvp.MvpPresenter;
import com.hannesdorfmann.mosby3.mvp.MvpView;
import com.paomi.client.R;
import com.paomi.client.bean.UserInfo;
import com.paomi.client.bean.UserUtils;
import com.paomi.client.utils.MyPermissionUtils;
import com.paomi.client.view.LoadingDialog;
import com.paomi.client.widget.CommonTitleView;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by LS on 2017/12/14
 * MVP 结合 Fragmentation
 * https://github.com/YoKeyword/Fragmentation
 */

public abstract class BaseMvpFragment<V extends MvpView, P extends MvpPresenter<V>> extends MvpFragment<V, P> implements CommonTitleView.OnTitleItemClickListener {
    protected static final String ARG_ITEM = "arg_item";

    protected View mView;
    protected Unbinder unbinder;
    public Typeface iconfont;

    public CommonTitleView getTitleView() {
        if (mTitleView != null) return mTitleView;
        FragmentActivity activity = getActivity();
        if (activity != null && activity instanceof BaseMvpActivity) {
            mTitleView = ((BaseMvpActivity) activity).getTitleView();
            return mTitleView;
        }
        return mTitleView;
    }

    private CommonTitleView mTitleView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView != null) return mView;
        initArguments();
        return inflater.inflate(getFragmentLayoutId(), container, false);
    }

    /**
     * 初始化Arguments
     */
    protected void initArguments() {

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        iconfont = Typeface.createFromAsset(getActivity().getAssets(), "iconfont.ttf");
        if (unbinder == null) unbinder = ButterKnife.bind(this, view);
        if (mView != null) return;
        mView = view;
        mTitleView = (CommonTitleView) mView.findViewById(R.id.title_view);
        if (mTitleView != null) mTitleView.setOnTitleItemClickListener(this);
        initEvent();
    }

    @NonNull
    protected abstract int getFragmentLayoutId();

    public UserInfo getUserInfo() {
        return UserUtils.getInstance().getUserInfo();
    }

    protected boolean getBooleanExtra(String key) {
        Bundle extras = getActivity().getIntent().getExtras();
        return extras != null && extras.getBoolean(key, false);
    }

    protected abstract void initEvent();

    /**
     * 标题右边点击事件，需要点击重写此方法
     */
    public void onRightClick(View view) {
    }

    /**
     * 标题左边点击事件，需要点击重写此方法
     */
    public void onLeftClick() {
    }

    /**
     * 标题点击事件，需要点击重写此方法
     */
    @Override
    public void onTitleClick() {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        MyPermissionUtils.onRequestPermissionsResult(requestCode, permissions, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LoadingDialog.get().hideLoading();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
            unbinder = null;
        }
    }
    /**
     * 判断是否有网
     * @return
     */
    public int getNetworkType() {
        int netType = 0;
        ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo == null) {
            return netType;
        }
        int nType = networkInfo.getType();
        if (nType == ConnectivityManager.TYPE_MOBILE) {
            netType = 1;
        } else if (nType == ConnectivityManager.TYPE_WIFI) {
            netType = 2;
        }
        return netType;
    }
}
